home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 1 / Cream of the Crop 1.iso / PROGRAM / DJINC106.ARJ / IOSTREAM.H < prev    next >
C/C++ Source or Header  |  1992-03-29  |  9KB  |  233 lines

  1. //    This is part of the iostream library, providing -*- C++ -*- input/output.
  2. //    Copyright (C) 1991 Per Bothner.
  3. //
  4. //    This library is free software; you can redistribute it and/or
  5. //    modify it under the terms of the GNU Library General Public
  6. //    License as published by the Free Software Foundation; either
  7. //    version 2 of the License, or (at your option) any later version.
  8. //
  9. //    This library is distributed in the hope that it will be useful,
  10. //    but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. //    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  12. //    Library General Public License for more details.
  13. //
  14. //    You should have received a copy of the GNU Library General Public
  15. //    License along with this library; if not, write to the Free
  16. //    Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  17.  
  18. #ifndef _IOSTREAM_H
  19. #ifdef __GNUG__
  20. #pragma interface
  21. #endif
  22. #define _IOSTREAM_H
  23. #define Q_ENV
  24.  
  25. #include <streambuf.h>
  26.  
  27. class istream; class ostream;
  28. typedef istream& (*__imanip)(istream&);
  29. typedef ostream& (*__omanip)(ostream&);
  30.  
  31. extern istream& ws(istream& ins);
  32. extern ostream& flush(ostream& outs);
  33. extern ostream& endl(ostream& outs);
  34. extern ostream& ends(ostream& outs);
  35.  
  36. class ostream : public ios
  37. {
  38.     void do_osfx();
  39.   public:
  40.     ostream();
  41.     ostream(streambuf* sb, ostream* tied=NULL);
  42.     ~ostream();
  43.  
  44.     int opfx() { if (!good()) return 0; if (_tie) _tie->flush(); return 1; }
  45.     void osfx() { if (flags() & (ios::unitbuf|ios::stdio))
  46.               do_osfx(); }
  47.     streambuf* ostreambuf() const { return _strbuf; }
  48.     ostream& flush();
  49.     ostream& put(char c);
  50.     ostream& write(const char *s, int n);
  51.     ostream& write(const unsigned char *s, int n) { return write((char*)s, n);}
  52.     ostream& write(const void *s, int n) { return write((char*)s, n);}
  53.     ostream& seekp(streampos);
  54.     ostream& seekp(streamoff, _seek_dir);
  55.     streampos tellp();
  56.     ostream& form(const char *format ...);
  57.     ostream& vform(const char *format, _G_va_list args);
  58. };
  59.  
  60. extern ostream& operator<<(ostream&, char c);
  61. inline ostream& operator<<(ostream& os, unsigned char c)
  62. { return os << (char)c; }
  63. //extern ostream& operator<<(ostream &os, signed char c) { return os << (char)c; }
  64. extern ostream& operator<<(ostream&, const char *s);
  65. inline ostream& operator<<(ostream& os, const unsigned char *s)
  66. { return os << (const char*)s; }
  67. //inline ostream& operator<<(ostream& os, const signed char *s)
  68. //{ return os << (const char*)s; }
  69. extern ostream& operator<<(ostream&, void *p);
  70. extern ostream& operator<<(ostream&, int n);
  71. extern ostream& operator<<(ostream&, long n);
  72. extern ostream& operator<<(ostream&, unsigned int n);
  73. extern ostream& operator<<(ostream&, unsigned long n);
  74. inline ostream& operator<<(ostream& os, short n) {return os << (int)n;}
  75. inline ostream& operator<<(ostream& os, unsigned short n)
  76. {return os << (unsigned int)n;}
  77. extern ostream& operator<<(ostream&, float n);
  78. extern ostream& operator<<(ostream&, double n);
  79. inline ostream& operator<<(ostream& os, __omanip func) { return (*func)(os); }
  80. extern ostream& operator<<(ostream&, streambuf*);
  81.  
  82. class istream : public ios
  83. {
  84.     _G_size_t _gcount;
  85.   public:
  86.     istream();
  87.     istream(streambuf* sb, ostream*tied=NULL);
  88.     ~istream();
  89.     streambuf* istreambuf() const { return _strbuf; }
  90.     istream& get(char& c);
  91.     istream& get(unsigned char& c);
  92.     istream& read(char *ptr, int n);
  93.     istream& read(unsigned char *ptr, int n) { return read((char*)ptr, n); }
  94.     istream& read(void *ptr, int n) { return read((char*)ptr, n); }
  95.     // Should get() and/or peek() set failbit and/or eofbit? FIXME!
  96.     istream& getline(char* ptr, int len, char delim = '\n');
  97.     istream& get(char* ptr, int len, char delim = '\n');
  98.     istream& gets(char **s, char delim = '\n');
  99.     int ipfx(int need) {
  100.     if (!good()) { set(ios::failbit); return 0; }
  101.     if (_tie && (need == 0 || rdbuf()->in_avail() < need)) _tie->flush();
  102.     if (!need && (flags() & ios::skipws) && !ws(*this)) return 0;
  103.     return 1;
  104.     }
  105.     int ipfx0() { // Optimized version of ipfx(0).
  106.     if (!good()) { set(ios::failbit); return 0; }
  107.     if (_tie) _tie->flush();
  108.     if ((flags() & ios::skipws) && !ws(*this)) return 0;
  109.     return 1;
  110.     }
  111.     int ipfx1() { // Optimized version of ipfx(1).
  112.     if (!good()) { set(ios::failbit); return 0; }
  113.     if (_tie && rdbuf()->in_avail() == 0) _tie->flush();
  114.     return 1;
  115.     }
  116.     int get() { if (!ipfx1()) return EOF;
  117.         int ch = _strbuf->sbumpc();
  118.         if (ch == EOF) set(ios::eofbit);
  119.         return ch; }
  120.     int peek() { if (!ipfx1()) return EOF;
  121.         int ch = _strbuf->sgetc();
  122.         if (ch == EOF) set(ios::eofbit);
  123.         return ch; }
  124.     _G_size_t gcount() { return _gcount; }
  125.     istream& ignore(int n=1, int delim = EOF);
  126.     istream& seekg(streampos);
  127.     istream& seekg(streamoff, _seek_dir);
  128.     streampos tellg();
  129.     istream& putback(char ch) {
  130.     if (good() && _strbuf->sputbackc(ch) == EOF) clear(ios::badbit);
  131.     return *this;}
  132.     istream& unget() {
  133.     if (good() && _strbuf->sungetc() == EOF) clear(ios::badbit);
  134.     return *this;}
  135. #ifdef _STREAM_COMPAT
  136.     istream& unget(char ch) { return putback(ch); }
  137.     int skip(int i);
  138. #endif
  139. };
  140.  
  141. extern istream& operator>>(istream&, char*);
  142. inline istream& operator>>(istream& is, unsigned char* p)
  143. { return is >> (char*)p; }
  144. //inline istream& operator>>(istream& is, signed char* p) { return is >> (char*)p; }
  145. extern istream& operator>>(istream&, char& c);
  146. extern istream& operator>>(istream&, unsigned char& c);
  147. //extern istream& operator>>(istream&, signed char& c);
  148. extern istream& operator>>(istream&, int&);
  149. extern istream& operator>>(istream&, long&);
  150. extern istream& operator>>(istream&, short&);
  151. extern istream& operator>>(istream&, unsigned int&);
  152. extern istream& operator>>(istream&, unsigned long&);
  153. extern istream& operator>>(istream&, unsigned short&);
  154. extern istream& operator>>(istream&, float&);
  155. extern istream& operator>>(istream&, double&);
  156. inline istream& operator>>(istream& is, __imanip func) { return (*func)(is); }
  157.  
  158. class iostream : public ios {
  159.     _G_size_t _gcount;
  160.   public:
  161.     iostream();
  162.     iostream(streambuf* sb, ostream*tied=NULL);
  163.     operator istream&() { return *(istream*)this; }
  164.     operator ostream&() { return *(ostream*)this; }
  165.     ~iostream();
  166.     // NOTE: These duplicate istream methods.
  167.     istream& get(char& c) { return ((istream*)this)->get(c); }
  168.     istream& get(unsigned char& c) { return ((istream*)this)->get(c); }
  169.     istream& read(char *ptr, int n) { return ((istream*)this)->read(ptr, n); }
  170.     istream& read(unsigned char *ptr, int n)
  171.     { return ((istream*)this)->read((char*)ptr, n); }
  172.     istream& read(void *ptr, int n)
  173.     { return ((istream*)this)->read((char*)ptr, n); }
  174.     istream& getline(char* ptr, int len, char delim = '\n')
  175.     { return ((istream*)this)->getline(ptr, len, delim); }
  176.     istream& get(char* ptr, int len, char delim = '\n')
  177.     { return ((istream*)this)->get(ptr, len, delim); }
  178.     istream& gets(char **s, char delim = '\n')
  179.     { return ((istream*)this)->gets(s, delim); }
  180.     istream& ignore(int n=1, int delim = EOF)
  181.     { return ((istream*)this)->ignore(n, delim); }
  182.     int ipfx(int need) { return ((istream*)this)->ipfx(need); }
  183.     int ipfx0()  { return ((istream*)this)->ipfx0(); }
  184.     int ipfx1()  { return ((istream*)this)->ipfx1(); }
  185.     int get() { return _strbuf->sbumpc(); }
  186.     int peek() { return ipfx1() ? _strbuf->sgetc() : EOF; }
  187.     _G_size_t gcount() { return _gcount; }
  188.     istream& putback(char ch) { return ((istream*)this)->putback(ch); }
  189.     istream& unget() { return ((istream*)this)->unget(); }
  190.     istream& seekg(streampos pos) { return ((istream*)this)->seekg(pos); }
  191.     istream& seekg(streamoff off, _seek_dir dir)
  192.     { return ((istream*)this)->seekg(off, dir); }
  193.     streampos tellg() { return ((istream*)this)->tellg(); }
  194. #ifdef _STREAM_COMPAT
  195.     istream& unget(char ch) { return putback(ch); }
  196. #endif
  197.  
  198.     // NOTE: These duplicate ostream methods.
  199.     int opfx() { return ((ostream*)this)->opfx(); }
  200.     void osfx() { ((ostream*)this)->osfx(); }
  201.     ostream& flush() { return ((ostream*)this)->flush(); }
  202.     ostream& put(char c) { return ((ostream*)this)->put(c); }
  203.     ostream& write(const char *s, int n)
  204.     { return ((ostream*)this)->write(s, n); }
  205.     ostream& write(const unsigned char *s, int n)
  206.     { return ((ostream*)this)->write((char*)s, n); }
  207.     ostream& write(const void *s, int n)
  208.     { return ((ostream*)this)->write((char*)s, n); }
  209.     ostream& form(const char *format ...);
  210.     ostream& vform(const char *format, _G_va_list args)
  211.     { return ((ostream*)this)->vform(format, args); }
  212.     ostream& seekp(streampos pos) { return ((ostream*)this)->seekp(pos); }
  213.     ostream& seekp(streamoff off, _seek_dir dir)
  214.     { return ((ostream*)this)->seekp(off, dir); }
  215.     streampos tellp() { return ((ostream*)this)->tellp(); }
  216. };
  217.  
  218. extern istream cin;
  219. extern ostream cout, cerr, clog; // clog->rdbuf() == cerr->rdbuf()
  220.  
  221. inline ostream& ostream::put(char c) { _strbuf->sputc(c); return *this; }
  222.  
  223. struct Iostream_init { } ;  // Compatibility hack for AT&T libraray.
  224.  
  225. inline ios& dec(ios& i)
  226. { i.setf(ios::dec, ios::dec|ios::hex|ios::oct); return i; }
  227. inline ios& hex(ios& i)
  228. { i.setf(ios::hex, ios::dec|ios::hex|ios::oct); return i; }
  229. inline ios& oct(ios& i)
  230. { i.setf(ios::oct, ios::dec|ios::hex|ios::oct); return i; }
  231.  
  232. #endif /*!_IOSTREAM_H*/
  233.